//
//  MainViewController.m
//  易车
//
//  Created by dllo on 15/11/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MainViewController.h"
#import "LunBoPicture.h"
#import "HeadTableViewCell.h"
#import "MainTableViewCell.h"
#import "MainModel.h"
#import "UIImageView+WebCache.h"

@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataSource;
@property (nonatomic, retain)UIView *headView;

@end

@implementation MainViewController

- (void)dealloc
{
    [_headView release];
    [_dataSource release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    
    [self.view addSubview:self.tableView];
    [self.tableView release];
    [self withDataSource];
    
      [self tableViewHeader];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    [self.view addSubview:view];
    [view release];
}

- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        self.headView = [[UIView alloc] init];
        self.headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200);
        self.headView.backgroundColor = [UIColor orangeColor];
        
        self.tableView.tableHeaderView = self.headView;
        [self.tableView registerClass:[HeadTableViewCell class] forCellReuseIdentifier:@"HeadTableViewCellReuseIdentifier"];
        [self.tableView registerClass:[MainTableViewCell class] forCellReuseIdentifier:@"MainTableViewCellReuseIdentifier"];
        [self.tableView release];
        [self.headView release];
    }
    return _tableView;
}

#define make -- 取数据
- (void)withDataSource{
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:@"/Users/dllo/Desktop/UI/易车/易车/YiChe.plist"];
    NSDictionary *dataDic = [dic objectForKey:@"data"];
    
    NSArray *array = [dataDic objectForKey:@"list"];
    for (NSDictionary *ltemDic in array) {
        MainModel *model = [[MainModel alloc] init];
        [model setValuesForKeysWithDictionary:ltemDic];
        [self.dataSource addObject:model];
        [model release];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 140;
    }else{
        return 80;
    }
}

#pragma mark -- HeaderView.ScrollView
- (void)tableViewHeader{
    LunBoPicture *lunBo = [[LunBoPicture alloc] initWithFrame:self.view.bounds withArray:[NSArray arrayWithObjects:@"1.png", @"2.png", @"3.png", @"4.png", nil]];
    [self.headView addSubview:lunBo];
    [lunBo release];
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MainModel *model = [[MainModel alloc] init];
    if (indexPath.row == 0) {
        HeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeadTableViewCellReuseIdentifier"];
        //    cell.label.text = @"FDS";
        return cell;
    }else{
        MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainTableViewCellReuseIdentifier"];
        model = [self.dataSource objectAtIndex:indexPath.row];
        [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.picCover]];
        cell.titleLabel.text = model.title;
        cell.nameLabel.text = model.src;
//            cell.numberLabel.text = model.commentCount;
        
        return cell;
    }
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
